//
//  NotificationUtil.swift
//  SwiftStudyDemoAll
//
//  Created by BINGO on 2019/5/12.
//  Copyright © 2019 mrlong. All rights reserved.
//

import Foundation

class NotificationUtil {
    // 1.定义一个闭包类型
    typealias swiftBlock = () -> Void
    
    // 2. 声明一个变量
    var callBack: swiftBlock?
    
    // 3. 定义一个方法,方法的参数为和swiftBlock类型一致的闭包,并赋值给callBack
    func callBackBlock(block: @escaping swiftBlock) {
        callBack = block
    }
    func send() -> Void {
        //设置推送内容
        NotificationCenter.default.post(name: NSNotification.Name.EndTopicSuccessNoti, object: nil)
        // 添加通知监听
        NotificationCenter.default.addObserver(self, selector: #selector(onClick), name: NSNotification.Name.EndTopicSuccessNoti, object: nil)
    }
    
    @objc func onClick() -> Void {
        //4. 调用闭包,设置你想传递的参数,调用前先判定一下,是否已实现
        if callBack != nil {
            callBack!()
        }
    }
    
}
extension Notification.Name {
    /// 结束会话成功通知
    static let EndTopicSuccessNoti = Notification.Name(rawValue:"EndTopicSuccess")
}
